package com.gitee.feizns.quickstart.ding.talk.api.v10.yida.forms.instances;

import com.aliyun.dingtalkyida_1_0.Client;
import com.aliyun.dingtalkyida_1_0.models.BatchSaveFormDataHeaders;
import com.aliyun.dingtalkyida_1_0.models.BatchSaveFormDataRequest;
import com.aliyun.dingtalkyida_1_0.models.DeleteFormDataHeaders;
import com.aliyun.dingtalkyida_1_0.models.DeleteFormDataRequest;
import com.aliyun.teautil.models.RuntimeOptions;
import com.gitee.feizns.quickstart.ding.talk.client.Clients;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;


/**
 * 批量创建表单实例（/v1.0/yida/forms/instances/batchSave）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/create-multiple-form-instances">批量创建表单实例（/v1.0/yida/forms/instances/batchSave）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/create-multiple-form-instances">服务端API 宜搭应用开发 表单 批量创建表单实例</a>
 * @author feizns
 * @since 2024/3/18
 */
public class BatchSave {

    /**
     * 根据表单实例编号删除表单实例数据
     * @param accessToken {@link String}
     * @param req {@link DeleteFormDataRequest}
     */
    @SneakyThrows
    public static List<String> execute(@NotNull String accessToken,
                               @NotNull BatchSaveFormDataRequest req) {
        Client client = Clients.get(Client.class);
        return client.batchSaveFormDataWithOptions(req, headers(accessToken), new RuntimeOptions()).getBody().getResult();
    }

    /**
     * 根据表单实例编号删除表单实例数据
     * @param accessToken 访问令牌
     * @param appType 应用类型
     * @param systemToken 系统令牌
     * @param userId 用户编号
     * @param formUuid 表格 UUID
     * @param formDataJsonList 表单数据JSON列表
     * @param noExecuteExpression 无执行表达式
     * @param asynchronousExecution 异步执行
     * @param keepRunningAfterException 异常后继续运行
     * @return {@link List}<{@link String}>
     */
    @SneakyThrows
    public static List<String> execute(@NotNull String accessToken,
                               @NotNull String appType,
                               @NotNull String systemToken,
                               @NotNull String userId,
                               @NotNull String formUuid,
                               @NotNull List<String> formDataJsonList,
                               @Nullable Boolean noExecuteExpression,
                               @Nullable Boolean asynchronousExecution,
                               @Nullable Boolean keepRunningAfterException) {
        BatchSaveFormDataRequest req = new BatchSaveFormDataRequest();
        req.setAppType(appType);
        req.setSystemToken(systemToken);
        req.setUserId(userId);
        req.setFormUuid(formUuid);
        req.setFormDataJsonList(formDataJsonList);
        req.setNoExecuteExpression(noExecuteExpression);
        req.setAsynchronousExecution(asynchronousExecution);
        req.setKeepRunningAfterException(keepRunningAfterException);
        return execute(accessToken, req);
    }

    /**
     * 请求头
     * @param accessToken 访问凭据
     * @return {@link DeleteFormDataHeaders}
     */
    public static BatchSaveFormDataHeaders headers(String accessToken) {
        BatchSaveFormDataHeaders headers = new BatchSaveFormDataHeaders();
        headers.xAcsDingtalkAccessToken = accessToken;
        return headers;
    }

}
